package com.noah.project.controller

import com.noah.project.global.Const
import com.noah.project.global.JsonManager
import com.noah.project.dao.BookDao
import com.noah.project.dao.NoteDao
import com.noah.project.global.TokenManager
import com.noah.project.model.Note
import com.noah.project.model.Reply
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class NoteApiController {
    @Autowired
    lateinit var bookDao: BookDao
    @Autowired
    lateinit var noteDao: NoteDao
    val log = LoggerFactory.getLogger(NoteApiController::class.simpleName)
    /*****************日记******************/
    @RequestMapping(Const.API_ADD_NOTE)
    fun addNote(note: Note, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            if (note.bid != null) {
                val retBook = bookDao.findById(note.bid)
                if (retBook == null) {
                    return Reply(code = Const.FAIL_NONE, msg = "日记本不存在")
                }
                note.nTime = System.currentTimeMillis()
                val retBid = noteDao.insert(note)
                if (retBid > 0) {
                    return Reply(code = Const.SUCCESS, msg = "发布日记成功", data = JsonManager.toJson(note))
                }
            }
            return Reply(code = Const.FAIL, msg = "发布日记失败")
        } catch (e: Exception) {
            log.error("创建日记", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "日记异常")
        }
    }

    @RequestMapping(Const.API_DELETE_NOTE)
    fun deleteNote(nid: Long, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val ret = noteDao.delete(nid)
            if (ret > 0) {
                return Reply(code = Const.SUCCESS, msg = "删除成功")
            }
            return Reply(code = Const.FAIL, msg = "删除失败")
        } catch (e: Exception) {
            log.error("删除日记", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "日记异常")
        }
    }

    @RequestMapping(Const.API_UPDATE_NOTE)
    fun updateNote(note: Note, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val ret = noteDao.update(note)
            if (ret > 0) {
                val retNote = noteDao.findById(note.bid)
                return Reply(code = Const.SUCCESS, msg = "修改成功", data = JsonManager.toJson(retNote))
            }
            return Reply(code = Const.FAIL, msg = "修改失败")
        } catch (e: Exception) {
            log.error("更新日记", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "日记异常")
        }
    }

    @RequestMapping(Const.API_GET_NOTE_LIST)
    fun getNoteList(bid: Long, time: Long, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            if (bid > 0) {//给bid,返回当前time之前的,该日记本内的日记,最新的在前面
                val retNotes = noteDao.findNotesOfBook(bid, time)
                if (retNotes.isNotEmpty()) {
                    return Reply(code = Const.SUCCESS, msg = "获取日记列表成功", data = JsonManager.toJson(retNotes))
                }
            } else {//不给bid,返回当前time之前的日记,最新的在前面
                val retNotes = noteDao.findNotesOfAll(time)
                if (retNotes.isNotEmpty()) {
                    return Reply(code = Const.SUCCESS, msg = "获取日记列表成功", data = JsonManager.toJson(retNotes))
                }
            }
            return Reply(code = Const.FAIL, msg = "没有更多日记")
        } catch (e: Exception) {
            log.error("日记列表", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "日记异常")
        }
    }

    @RequestMapping(Const.API_GET_NOTE)
    fun getNote(bid: Long, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val retNote = noteDao.findById(bid)
            if (retNote != null) {
                return Reply(code = Const.SUCCESS, msg = "获取日记成功", data = JsonManager.toJson(retNote))
            }
            return Reply(code = Const.FAIL, msg = "找不到该日记")
        } catch (e: Exception) {
            log.error("单个日记", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "日记异常")
        }
    }
}